<!--新增短信模板-->
<template>
  <section class="webview fixHeight">
    <el-scrollbar style="height:100%">
      <el-form ref="form" :model="detail" label-width="100px" class="form">
        <el-form-item label="内容">
          <el-input v-model="detail.content" />
        </el-form-item>
        <el-form-item label="短信通道号">
          <el-input v-model="detail.sender" />
        </el-form-item>
        <el-form-item label="签名名称">
          <el-input v-model="detail.signature" />
        </el-form-item>
        <el-form-item label="序列号">
          <el-input v-model="detail.simplify" />
        </el-form-item>
        <el-form-item label="模板类型">
          <el-input v-model="detail.templateType" />
        </el-form-item>
        <el-form-item label="是否审核">
          <el-radio v-model="detail.isAudit" :label="true">是</el-radio>
          <el-radio v-model="detail.isAudit" :label="false">否</el-radio>
        </el-form-item>
        <el-form-item label="第三方模板id">
          <el-input v-model="detail.thirdId" />
        </el-form-item>
        <el-form-item label="标题">
          <el-input v-model="detail.title" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="cancel">返回</el-button>
        </el-form-item>
      </el-form>
    </el-scrollbar>
  </section>
</template>
<script>
import { templateAdd, templateUpdate, templateDetail } from '@/api/api'
export default {
  data() {
    return {
      detail: {
        'content': '',
        'isAudit': true,
        'sender': '',
        'signature': '',
        'simplify': '',
        'templateType': '',
        'thirdId': '',
        'title': ''
      }
    }
  },
  created() {
    if (this.$route.query.id) {
      templateDetail(this.$route.query.id).then(({ data }) => {
        this.detail = data
      })
    }
  },
  methods: {
    // 添加/编辑
    submit() {
      // 验证不通过则不提交
      if (!this.vertify()) {
        return false
      }
      if (this.$route.query.id) {
        // 编辑
        templateUpdate(this.detail).then(data => {
          Is.tip('编辑成功', 2000, 'success')
          this.$router.back()
        })
      } else {
        // 新增
        templateAdd(this.detail).then(data => {
          Is.tip('添加成功', 2000, 'success')
          this.$router.back()
        })
      }
    },
    // 返回
    cancel() {
      this.$router.back()
    },
    vertify() {
      if (Is.empty(this.detail.content)) {
        Is.tip('请输入内容')
        return false
      }
      if (Is.empty(this.detail.sender)) {
        Is.tip('请输入短信通道号')
        return false
      }
      if (Is.empty(this.detail.signature)) {
        Is.tip('请输入签名名称')
        return false
      }
      if (Is.empty(this.detail.simplify)) {
        Is.tip('请输入序列号')
        return false
      }
      if (Is.empty(this.detail.title)) {
        Is.tip('请输入标题')
        return false
      }
      return true
    }
  }}
</script>
